package com.api.nosession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.system.common.Result;
import com.system.security.IsSecurity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

/**
 * @Classname CacheApi
 * @Description TODO
 * @Date 2023/10/4 11:23
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@IsSecurity(isSecurity = false)
@RestController
@RequestMapping("/cache")
public class CacheApi {

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private Caffeine<Object, Object> caffeineConfig;

    @GetMapping("/getallcache")
    public JSONObject getAllCache(){



        JSONArray array=new JSONArray();
        Set<String> cacheNames = (Set<String>) cacheManager.getCacheNames();
        for (String cacheName : cacheNames) {
            JSONObject object = new JSONObject();
            Cache cache = cacheManager.getCache(cacheName);
            Object val = cache.getNativeCache(); // 获取缓存内容
            System.out.println("Cache Name: " + cacheName);
            System.out.println("Cache Content: " + JSONObject.toJSONString(val));
            object.put("key",cacheName);
            object.put("value",JSONObject.toJSONString(val));
            array.add(object);
        }
        return Result.successInfo(array);
    }
}
